// pages/myActive/myActive.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var page=1;
var last_page;
var activeIndex=99;
var addressId;
var orderId;
var formid;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'my/treasure',
      data: { type: activeIndex, page: page },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        var shops = res.data.data.data;
        var bill = "bill"
        for (var i = 0; i < shops.length; i++) {
          console.log(i)
          var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
          shops[i].bill = billF.toFixed(2) + "%"
        }
        that.setData({
          listItem: that.data.listItem.concat(shops)
        });
        page++;
        wx.hideLoading();
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar:[
      { name: "全部", id: "99" },
      { name: "待揭晓", id: "1" },
      { name: "已揭晓", id: "3" },
      { name: "已获得", id: "5" },
      { name: "已失效", id: "4" }
    ],
    activetab: 0,
    activeIndex: 99,
    listItem:[],
    adr:0,
    addressId:"",
    is_adr:0,
    queS:true,
    meidenglu:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    
  },
  onChange(e) {
    console.log(e)
    token = wx.getStorageSync("token")
    var that=this;
    var idx = e.detail.index;
    var activetab = that.data.activetab;
    if (idx == activetab){
      return
    }else{
      if (idx == 0) {
        activeIndex = 99;
        that.setData({
          activetab:idx,
          listItem:[]
        })
        that.getlist()
      } else if (idx == 1){
        activeIndex = 1;
        that.setData({
          activetab: idx,
          listItem: []
        })
        that.getlist()
      } else if (idx == 2){
        activeIndex = 3;
        that.setData({
          activetab: idx,
          listItem: []
        })
        that.getlist()
      } else if (idx == 3){
        activeIndex = 5;
        that.setData({
          activetab: idx,
          listItem: []
        })
        that.getlist()
      }else{
        activeIndex = 4;
        that.setData({
          activetab: idx,
          listItem: []
        })
        that.getlist()
      }
    }
    
    
  },
  refund:function(e){
    var that=this;
    var id=e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item;
    var jsonItem = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/myAfterSale/myAfterSale?item=' + jsonItem,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  queAdr:function(e){
    orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/myadr/myadr?type=2&orderId=' + orderId,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  toShaidan:function(e){
     var that=this;
    var item = e.currentTarget.dataset.item;
    var JsonItem = JSON.stringify(item)
     wx.navigateTo({
       url: '/pages/toShaidan/toShaidan?item=' + JsonItem,
     })
  },

  getlist(){
    var that=this;
    if(page>1){
      page=1
    }
    wx.request({
      url: url.URl + 'my/treasure',
      data: { type: activeIndex, page:page},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;

        wx.hideLoading()
        if (res.data.code == 200) {
          
          last_page = res.data.data.last_page;

          var shops = res.data.data.data;
          var bill = "bill"
          for (var i = 0; i < shops.length; i++) {
            console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100;
            shops[i].bill = billF.toFixed(2) + "%"
          }
          that.setData({
            listItem: shops
          })
        }else if(res.data.code==201){
           that.setData({
             queS:false
           })
        }else if(res.data.code==401){
          that.setData({
            meidenglu:false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  toDenglu:function(e){
    var that = this;
    token = wx.getStorageSync("token")
    console.log(e.detail.formId);
    formid = e.detail.formId
    this.setData({
      formid: formid
    })
    app.onGotUserInfo(e, that, formid)
    that.setData({
      meidenglu:true
    })
  },
  queOrder:function(e){
    var that = this;
    var orderId=e.currentTarget.dataset.id;
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/treasure/confirm/order',
      data: { id: orderId},
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
        if (res.data.code == 200) {
          that.getlist()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var addressId = that.data.addressId;
    console.log(addressId)
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data
        that.getlist()
      },
    })
  },
  logCheck(token) {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'login/check',
      data: {},
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        var msg = res.data.msg;
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 200) {



        } else if (res.data == 401) {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  
   /**
   * 生命周期函数--监听页面卸载
   */

  onUnload: function () {
    page = 1;
    var that=this
    console.log(123456789)
    activeIndex = 99;
    that.setData({
      addressId:0
    })
  },
  onPullDownRefresh: function () {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      page = 1;
      self.getlist()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})